﻿// ==============================================================================
// Created by Bndy at 4 / 13 / 2008
// Copyright (c) 2008 Bndy, All rights reserved.
// Welcome to my site http : // www.bndy.net
//
//                    * * * * * * * * * * * * * 
//                    * MAIL- bndy5337@163.com *       http://www.bndy.net
//                    * Q Q - 81795705 =========================================>
//                    * MSN - bndy5337@msn.com *
//                    * * * * * * * * * * * * * 
//
// ------------------------------------------------------------------------------
// String 相关函数
// ==============================================================================

String.prototype.trim = function() {
    return this.replace(/(^\s+)|(\s+)$g/, "");
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/g, "");
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/g, "");
}

/************************************ 判断 *********************************/

String.prototype.isEmpty = function() {
    if (this.length <= 0) return true; return false;
}

String.prototype.isNumeric = function() {
    var pattern = /^[-]?\d+$/;
    return pattern.test(this);
}

String.prototype.isEmail = function() {
    var pattern = /[\w][\w-\.]*@[\w][\w-_]*\.[\w][\w\.]+/;
    return pattern.test(this);
}

String.prototype.isDecimal = function() {
    var pattern = /^[-]?\d+(\.\d+)?$/;
    return pattern.test(this);
}

String.prototype.isDate = function() {
    var pattern = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s+(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d\s*)?$/;
    return pattern.test(this);
}

String.prototype.isTel = function() {
    var pattern = /^\d{3,4}-\d{7,8}$/;
    return pattern.test(this);
}

String.prototype.isMobileNo = function() {
    var pattern = /^1\d{10}$/;
    return pattern.test(this);
}

String.prototype.equals = function() {
    for (var i = 0; i < arguments.length; i++)
        if (this == arguments[i])
        return true;
    return false;
}

/************************************ 其它函数 *********************************/

String.prototype.getLength = function() {
    var str, Num = 0;
    for (var i = 0; i < this.length; i++) {
        str = this.substring(i, i + 1);
        if (str <= "~") //对双字节字特殊处理
            Num += 1;
        else
            Num += 2;
    }
    return Num;
}

String.prototype.cutString = function (len, replaceString) {
    var n = 0;
    var s = '';
    var i = 0;

    for (i = 0; i < this.length; i++) {
        if (this.charCodeAt(i) > 127) {
            n += 2;
        }
        else {
            n++;
        }

        s = s + this.charAt(i);
        if (n >= len)
            break;
    }

    if (replaceString != undefined  && i < this.length - 1)
        s = s + replaceString;
    return s;
}

/************************************ ---- *********************************/

